package com.ten.teacher.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ten.common.core.entity.EduTeacher;
import com.ten.common.core.entity.TbCheckTeacher;
import com.ten.common.core.vo.Result;
import com.ten.teacher.dao.EduTeacherDao;
import com.ten.teacher.dao.TbCheckTeacherDao;
import com.ten.teacher.service.EduTeacherService;
import com.ten.teacher.vo.BeTeacherFromTbMemberVo;
import com.ten.teacher.vo.TeacherListVo;
import com.ten.teacher.vo.TeacherStatusVo;
import com.ten.teacher.vo.TeacherUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author ：Wqw
 * @date ：Created in 2022/12/7 15:40
 * @description：查询讲师列表的serviceImpl层
 * @modified By：
 * @version:
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherDao, EduTeacher> implements EduTeacherService {
    @Autowired
    private EduTeacherDao eduTeacherDao;
    @Autowired
    private TbCheckTeacherDao tbCheckTeacherDao;

    /**
     * 查询讲师列表,模糊查询,分页
     * page 页码
     * limit 每页显示行数
     * TeacherListVo 条件类 用来封装模糊查询和分页
     */
    @Override
    public Result<IPage<EduTeacher>> selectTeacherList(Integer page, Integer limit, TeacherListVo teacherListVo) {
        IPage<EduTeacher> iPage = new Page<>(page, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(teacherListVo.getName())) {
            wrapper.like("name", teacherListVo.getName());
        }
        if (StringUtils.hasText(teacherListVo.getMobile())) {
            wrapper.like("mobile", teacherListVo.getMobile());
        }
        if (StringUtils.hasText(teacherListVo.getStatus())) {
            wrapper.eq("status", teacherListVo.getStatus());
        }
        wrapper.eq("is_deleted", 0);
        IPage<EduTeacher> eduTeacherIPage = eduTeacherDao.selectPage(iPage, wrapper);
        return new Result<>(200, "查询讲师成功！", eduTeacherIPage);
    }

    /**
     * 修改讲师信息
     * TeacherUpdateVo 条件类
     */
    @Override
    @Transactional
    public Result<EduTeacher> updateTeacherById(TeacherUpdateVo teacherUpdateVo) {
        //创建一个新老师接收修改条件
        EduTeacher newTeacher = new EduTeacher();
        //设置修改条件
        UpdateWrapper<EduTeacher> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", teacherUpdateVo.getId());
        wrapper.set("mobile", teacherUpdateVo.getMobile());
        wrapper.set("name", teacherUpdateVo.getName());
        wrapper.set("email", teacherUpdateVo.getEmail());
        wrapper.set("intro", teacherUpdateVo.getIntro());
        wrapper.set("gmt_modified", LocalDateTime.now());
        //仅修改指定的列
        int update = eduTeacherDao.update(newTeacher, wrapper);
        if (update == 1) {
            return new Result<>(200, "修改信息成功！");
        }
        return new Result<>(500, "修改信息失败！");
    }


    /**
     * 更新讲师状态
     * 未通过审核的讲师不能改变状态
     * 1，根据讲师id连表查询讲师审核状态
     * 2，根据审核状态判断是否能够修改讲师状态
     * TeacherStatusVo 条件类
     */
    @Override
    @Transactional
    public Result<EduTeacher> updateStatus(TeacherStatusVo teacherStatusVo) {
        //1.通过讲师id连表查询讲师状态
        String id = teacherStatusVo.getId();
        EduTeacher teacher = eduTeacherDao.queryById(id);
        String checkStatus = teacher.getCheckStatus();
        //2.修改讲师状态
        //创建一个新老师接收修改条件
        EduTeacher newTeacher = new EduTeacher();
        //设置修改条件
        UpdateWrapper<EduTeacher> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        String status = teacherStatusVo.getStatus();
        //判断讲师是否被审核
        if (StringUtils.hasText(checkStatus) && "1".equals(checkStatus)) {
            wrapper.set("status", status);
            wrapper.set("gmt_modified", LocalDateTime.now());
            eduTeacherDao.update(newTeacher, wrapper);
            return new Result<>(200, "修改状态成功！");
        }
        return new Result<>(500, "修改状态失败！");
    }


    /**
     * 会员申请讲师
     * 需要远程调用讲师微服务中的方法
     * 将编译的属性放入讲师列表不显示，
     * 审核列表中状态为待审核
     */
    @Override
    public Result<EduTeacher> addTeacher(BeTeacherFromTbMemberVo beTeacherFromTbMemberVo) {
        //讲师列表需要添加的数据
        EduTeacher eduTeacher = new EduTeacher();
        long teacherId = IdUtil.getSnowflakeNextId();
        eduTeacher.setId(teacherId + "");
        eduTeacher.setName(beTeacherFromTbMemberVo.getName());
        eduTeacher.setIntro(beTeacherFromTbMemberVo.getIntro());
        eduTeacher.setLevel(beTeacherFromTbMemberVo.getLevel());
        eduTeacher.setAvatar(beTeacherFromTbMemberVo.getAvatar());
        eduTeacher.setIsDeleted(1);
        eduTeacher.setGmtCreate(LocalDateTime.now());
        eduTeacher.setGmtModified(LocalDateTime.now());
        eduTeacher.setMobile(beTeacherFromTbMemberVo.getMobile());
        eduTeacher.setEmail(beTeacherFromTbMemberVo.getEmail());
        eduTeacher.setStatus("1");
        eduTeacherDao.insert(eduTeacher);
        //审核讲师列表需要添加的数据
        TbCheckTeacher tbCheckTeacher = new TbCheckTeacher();
        tbCheckTeacher.setTeacherId(teacherId + "");
        tbCheckTeacher.setCheckStatus("2");
        tbCheckTeacher.setCheckContent(beTeacherFromTbMemberVo.getIntro());
        tbCheckTeacher.setCheckTime(LocalDateTime.now());
        tbCheckTeacherDao.insert(tbCheckTeacher);
        return new Result<>(200, "成功提交审核,请耐心等待");
    }

/*************************************讲师中心************************************************/
    /**根据手机号修改讲师信息*/
    @Override
    public Result<EduTeacher> updateTeacherByMember(TeacherUpdateVo teacherUpdateVo) {
        UpdateWrapper<EduTeacher> wrapper = new UpdateWrapper<>();
        wrapper.eq("mobile",teacherUpdateVo.getMobile());
        wrapper.set("avatar",teacherUpdateVo.getAvatar());
        wrapper.set("name",teacherUpdateVo.getName());
        wrapper.set("intro",teacherUpdateVo.getIntro());

        int update = eduTeacherDao.update(null, wrapper);
        if (update==1){
           return new Result<>(2000,"修改讲师信息成功");
        }
        return new Result<>(5000,"修改讲师失败");
    }
/*************************************供远端调用的微服务************************************************/

    /**根据讲师手机查询讲师信息*/
    @Override
    public Result<EduTeacher> getTeacherByMobile(String mobile) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        EduTeacher eduTeacher = eduTeacherDao.selectOne(wrapper);
        return new Result<>(200,"根据讲师手机号，成功查询到讲师信息",eduTeacher);
    }













    /**
     * 根据讲师id查询讲师信息
     * @param id
     * @return
     */
    @Override
    public Result<EduTeacher> getTeacherById(String id) {
        EduTeacher eduTeacher = eduTeacherDao.selectById(id);
        return new Result<>(200,"根据老师id，成功查询到讲师信息",eduTeacher);
    }
}
